﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GameLib1
{
    class Map
    {
        public enum TILE
        {
            SPAWN_A,
            SPAWN_B,
            TURRET_SPAWN,
            BASE_TOPLEFT,
            BASE_TOPRIGHT,
            BASE_BOTTOMLEFT,
            BASE_BOTTOMRIGHT,
            FLOOR_GRASS,
            FLOOR_STONE,
            WALL,
        }

        TILE [][]mData ;

        public int mWidth = 0, mHeight = 0;

        public void Create(int width, int height, TILE fill_with)
        {
            mData = new TILE[height][] ;

            for (int i = 0; i < height; i++)
            {
                mData[i] = new TILE[width];

                for (int j = 0; j < width; j++)
                {
                    mData[i][j] = fill_with;
                }
            }

            mWidth = width;
            mHeight = height;

        }

        public Map(String file_name)
        {
            if (file_name == null)
                return;


            String [] buffer = System.IO.File.ReadAllLines(file_name);

            mData = new TILE[buffer.Length][] ;
            int i = 0;
            foreach (String str in buffer)
            {
                String [] sub_str = str.Split(' ');

                mData[i] = new TILE[sub_str.Length];

                int j = 0;
                foreach (String s in sub_str)
                {
                    int id = int.Parse(s);
                    mData[i][j++] = (TILE)id;
                }
                i++ ;
            }
            mWidth = mData[0].Length;
            mHeight = mData.Length;

        }

        public TILE this[int x, int y]
        {
            get
            {
                return mData[y][x];
            }
            set
            {
                mData[y][x] = value;
            }
        }

        public void Write( String file_name )
        {
            String[] to_write = new String[mData.Length] ;
            int index = 0;

            foreach (TILE[] t_array in mData)
            {
                String str = "" ;
                foreach (TILE t in t_array)
                {
                    str = str + " " + ((int)t).ToString();
                }
                to_write[index++] = str;
            }

            System.IO.File.WriteAllLines(file_name, to_write);
        }

    }
}
